﻿@using FujiyBlog.Core.DomainObjects
@using FujiyBlog.Core.Extensions
@using FujiyBlog.Web.Common
@model FujiyBlog.Web.Areas.Admin.ViewModels.AdminPostIndex

@{
    Layout = MVC.Admin.Post.Views._Layout;
    ViewBag.Title = "Posts";
}

<h2>Posts</h2>

@if (User.IsInRole(Role.CreateNewPosts))
{
    <p>
        @Html.ActionLink("Write new post", MVC.Admin.Post.Edit())
    </p>
}
    

<div id="posts-type-filters">
@Html.ActionLink("All", MVC.Admin.Post.Index()) | @Html.ActionLink("Drafts", MVC.Admin.Post.Index(null, false))| @Html.ActionLink("Published", MVC.Admin.Post.Index(null, true))
</div>

@Pager.Links(Model.CurrentPage, Model.TotalPages, Resources.Labels.NextPage, Resources.Labels.PreviousPage)

<table>
    <tr>
        <th>
            Published
        </th>
        <th>
            Title
        </th>
        <th>
            Author
        </th>
        <th>
            Categories
        </th>
        <th>
            Tags
        </th>
        <th>
            CreationDate
        </th>
        <th>
            PublicationDate
        </th>
        <th>
            Comments
        </th>
        <th></th>
    </tr>

@foreach (var item in Model.RecentPosts)
{
    <tr id="item-row-@item.Post.Id">
        <td class="@(item.Post.IsPublished ? "published" : "notpublished")">
            @Html.DisplayFor(modelItem => item.Post.IsPublished)
        </td>
        <td>
            @Html.ActionLink(item.Post.Title, MVC.Post.Details(item.Post.Slug))
        </td>
        <td>
            @(item.Post.Author.DisplayName ?? item.Post.Author.Username)
        </td>
        <td>
            @string.Join(", ", item.Post.Categories.Select(x => x.Name).ToList())
        </td>
        <td>
            @string.Join(", ", item.Post.Tags.Select(x => x.Name).ToList())
        </td>
        <td>
            @DateTimeUtil.ConvertUtcToMyTimeZone(item.Post.CreationDate)
        </td>
        <td>
            @DateTimeUtil.ConvertUtcToMyTimeZone(item.Post.PublicationDate)
        </td>
        <td>
            @Html.DisplayFor(modelitem => item.CommentsTotal)
        </td>
        <td>
        @if ((item.Post.Author.Username == User.Identity.Name && User.IsInRole(Role.EditOwnPosts)) || User.IsInRole(Role.EditOtherUsersPosts))
        {
            @Html.ActionLink("Edit", MVC.Admin.Post.Edit(item.Post.Id))
        }
        @if ((item.Post.Author.Username == User.Identity.Name && User.IsInRole(Role.DeleteOwnPosts)) || (item.Post.Author.Username != User.Identity.Name && User.IsInRole(Role.DeleteOtherUsersPosts)))
        {
            <text>
            | @Ajax.ActionLink("Delete", MVC.Admin.Post.Delete(item.Post.Id), new AjaxOptions { HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "RemoveItemRow(" + item.Post.Id + ");" })
            </text>
        }
        </td>
    </tr>
}
</table>

<script type="text/javascript">
    function RemoveItemRow(itemId) {
        $('#item-row-' + itemId).hide();
    }
    
</script>

@Pager.Links(Model.CurrentPage, Model.TotalPages, Resources.Labels.NextPage, Resources.Labels.PreviousPage)
